using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace SocketTool.Model.BindingModels
{
    public class ReqParamBindingModel : NotifyObject
    {
        public ReqParamBindingModel() : this(null)
        {
            
        }

        public ReqParamBindingModel(ReqParamBindingModel parent)
        {
            this.Parent = parent;
            if (parent != null)
                this.Level = parent.Level + 1;
            this.Children = new ObservableCollection<ReqParamBindingModel>();
        }

        public int Level { get; }
        
        private bool mHasChild = false;
        /// <summary>
        /// 是否包含子元素
        /// </summary>
        public bool HasChild
        {
            get => this.mHasChild;
            set => Set(ref this.mHasChild, value);
        }
        
        private bool mIsExpanded = true;
        /// <summary>
        /// 是否展开
        /// </summary>
        public bool IsExpanded
        {
            get => this.mIsExpanded;
            set => Set(ref this.mIsExpanded, value);
        }

        private string mKey;
        public string Key
        {
            get => this.mKey;
            set => Set(ref this.mKey, value);
        }

        private string mValue;
        public string Value
        {
            get => this.mValue;
            set => Set(ref this.mValue, value);
        }

        private bool mCanInputValue = true;
        public bool CanInputValue
        {
            get => this.mCanInputValue;
            private set => Set(ref this.mCanInputValue, value);
        }

        private int mValueType = (int)Enums.JsonValueTypeEnum.String;
        public int ValueType
        {
            get => this.mValueType;
            set
            {
                Set(ref this.mValueType, value); 
                if(value == (int)Enums.JsonValueTypeEnum.Object || value == (int)Enums.JsonValueTypeEnum.ObjectArray)
                {
                    this.Value = string.Empty;
                    this.CanInputValue = false;
                }
                else
                {
                    this.CanInputValue = true;
                    this.Children.Clear();
                }
            }
        }

        private bool mIsChecked = true;
        /// <summary>
        /// 选中表示可用 未选中则在生成字符串时自动过滤
        /// </summary>
        public bool IsChecked
        {
            get => this.mIsChecked;
            set
            {
                Set(ref this.mIsChecked, value);
                if (!value && this.Children.Count > 0)
                {
                    foreach (var item in this.Children)
                    {
                        item.IsChecked = value;
                    }
                }
            }
        }

        private string mExplan;
        /// <summary>
        /// 字段描述
        /// </summary>
        public string Explan
        {
            get => this.mExplan;
            set => Set(ref this.mExplan, value);
        }
        
        public ReqParamBindingModel Parent { get; set; }
        public ObservableCollection<ReqParamBindingModel> Children { get; private set; }
    }
}